package bscorp.appbase.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.listener.DialogUIListener;

import java.util.Date;
import java.util.List;

import bscorp.appbase.AppManager;
import bscorp.appbase.R;
import bscorp.appbase.adapter.OperateAdapter;
import bscorp.appbase.bmo.DeviceBmo;
import bscorp.appbase.bmo.OperateBmo;
import bscorp.appbase.bmo.OrderBmo;
import bscorp.appbase.divider.EnvironmentMonitorItemDecoration;
import bscorp.appbase.model.Device;
import bscorp.appbase.model.Operate;
import bscorp.appbase.model.Order;
import bscorp.appbase.model.User;

public class OrderDetailActivity extends AppCompatActivity {

    Order mOrder;
    Button btn_submit;
    Button btn_refuse;
    RecyclerView rv_list;
    OperateAdapter mAdapter;
    List<Operate> mOperate;
    User mUser = AppManager.LOCAL_LOGINED_USER;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        DialogUIUtils.init(this);
        mOrder = (Order) getIntent().getSerializableExtra("order");

        btn_refuse = (Button) findViewById(R.id.btn_refuse);
        btn_refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                DialogUIUtils.showAlert(OrderDetailActivity.this, "驳回", "", "请输入驳回原因", "", "确定", "", false, true, true, new DialogUIListener() {
                    @Override
                    public void onPositive() {
                    }

                    @Override
                    public void onNegative() {
                    }

                    @Override
                    public void onGetInput(CharSequence input1, CharSequence input2) {
                        super.onGetInput(input1, input2);

                        mOrder.setType(Order.ORDER_AGAIN_REPAIRE);

                        if (!new OrderBmo().updateOrder(mOrder)) {
                            DialogUIUtils.showToastLong("操作失败，工单更新异常");
                            return;
                        }

                        Operate operate = new Operate();
                        operate.setOrderId(mOrder.getId());
                        operate.setUser(mUser.getName());
                        operate.setUserPhone(mUser.getPhone());
                        operate.setDescription(input1.toString());
                        operate.setTime(new Date());
                        operate.setResult("检修失败驳回工单");

                        if (new OperateBmo().saveOperate(operate)) {
                            DialogUIUtils.showToastLong("操作成功");
                        } else {
                            DialogUIUtils.showToastLong("操作失败，工单更新异常");
                        }

                        return;
                    }
                }).show();
            }
        });
        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mOrder.getType().equals(Order.ORDER_NEW)) {
                    mOrder.setType(Order.ORDER_WAIT_REPAIRE);
                    mOrder.setRepaireUserId(mUser.getId());
                    String operateResult = "接单成功";
                    if (!new OrderBmo().updateOrder(mOrder)) {
                        DialogUIUtils.showToastLong("操作失败，工单更新异常");
                        return;
                    }

                    Operate operate = new Operate();
                    operate.setOrderId(mOrder.getId());
                    operate.setUser(mUser.getName());
                    operate.setUserPhone(mUser.getPhone());
                    operate.setTime(new Date());
                    operate.setResult(operateResult);

                    if (new OperateBmo().saveOperate(operate)) {
                        DialogUIUtils.showToastLong("操作成功");
                        onBackPressed();
                    } else {
                        DialogUIUtils.showToastLong("操作失败，工单更新异常");
                    }
                    return;
                }
                String title = "备注";
                String desc = "请输入";
                switch (mOrder.getType()) {
                    case Order.ORDER_WAIT_REPAIRE:
                        title = "故障原因";
                        desc = "请输入" + title;
                        break;
                    case Order.ORDER_WAIT_CHECK:
                        title = "维修评价";
                        desc = "请输入" + title;
                        break;
                    case Order.ORDER_AGAIN_REPAIRE:
                        title = "故障原因";
                        desc = "请输入" + title;
                        break;
                }

                DialogUIUtils.showAlert(OrderDetailActivity.this, title, "", desc, "", "确定", "", false, true, true, new DialogUIListener() {
                    @Override
                    public void onPositive() {
                    }

                    @Override
                    public void onNegative() {
                    }

                    @Override
                    public void onGetInput(CharSequence input1, CharSequence input2) {
                        super.onGetInput(input1, input2);

                        String operateResult = "";
                        switch (mOrder.getType()) {
                            case Order.ORDER_WAIT_REPAIRE:
                                mOrder.setType(Order.ORDER_WAIT_CHECK);
                                operateResult = "检修完成";
                                break;
                            case Order.ORDER_WAIT_CHECK:
                                mOrder.setType(Order.ORDER_SUCCESS);
                                operateResult = "检修完成";
                                break;
                            case Order.ORDER_AGAIN_REPAIRE:
                                mOrder.setType(Order.ORDER_WAIT_CHECK);
                                operateResult = "检修完成";
                                break;
                        }

                        if (!new OrderBmo().updateOrder(mOrder)) {
                            DialogUIUtils.showToastLong("操作失败，工单更新异常");
                            return;
                        }

                        Operate operate = new Operate();
                        operate.setOrderId(mOrder.getId());
                        operate.setUser(mUser.getName());
                        operate.setUserPhone(mUser.getPhone());
                        operate.setDescription(input1.toString());
                        operate.setTime(new Date());
                        operate.setResult(operateResult);

                        if (new OperateBmo().saveOperate(operate)) {
                            DialogUIUtils.showToastLong("操作成功");
                            onBackPressed();
                        } else {
                            DialogUIUtils.showToastLong("操作失败，工单更新异常");
                        }

                        return;
                    }
                }).show();
            }
        });


        switch (mOrder.getType()) {
            case Order.ORDER_NEW:
                if (mUser.getId() == mOrder.getReportUserId()) {
                    btn_submit.setVisibility(View.GONE);
                }
                btn_submit.setText("抢单");
                break;
            case Order.ORDER_WAIT_REPAIRE:
                if (mUser.getId() != mOrder.getRepaireUserId()) {
                    btn_submit.setVisibility(View.GONE);
                }
                btn_submit.setText("提交");
                break;
            case Order.ORDER_WAIT_CHECK:
                if (mUser.getId() != mOrder.getReportUserId()) {
                    btn_submit.setVisibility(View.GONE);
                }
                btn_submit.setText("提交");
                if (mUser.getId() != mOrder.getRepaireUserId()) {
                    btn_refuse.setVisibility(View.VISIBLE);
                }
                break;
            case Order.ORDER_AGAIN_REPAIRE:
                if (mUser.getId() != mOrder.getRepaireUserId()) {
                    btn_submit.setVisibility(View.GONE);
                }
                btn_submit.setText("再次提交");
                break;
            case Order.ORDER_SUCCESS:
                btn_submit.setVisibility(View.GONE);
                break;
        }

        initListView();
    }

    private void initListView() {
        mOperate = new OperateBmo().getOperateByOrderId(mOrder.getId());

        rv_list = (RecyclerView) findViewById(R.id.rv_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(OrderDetailActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        mAdapter = new OperateAdapter(R.layout.activity_operate_item, mOperate);
        rv_list.setAdapter(mAdapter);
        rv_list.setItemAnimator(new DefaultItemAnimator());
        rv_list.addItemDecoration(new EnvironmentMonitorItemDecoration(OrderDetailActivity.this, 10));


        mAdapter.addHeaderView(getHeaderView());
    }

    private View getHeaderView() {
        Device device = new DeviceBmo().getDeviceById(mOrder.getDeviceId());

        View view = getLayoutInflater().inflate(R.layout.activity_order_detail_head, (ViewGroup) rv_list.getParent(), false);
        TextView tv_device_name = (TextView) view.findViewById(R.id.tv_device_name);
        tv_device_name.setText(device.getName());
        TextView tv_device_code = (TextView) view.findViewById(R.id.tv_device_code);
        tv_device_code.setText(device.getCode());
        TextView tv_device_location = (TextView) view.findViewById(R.id.tv_device_location);
        tv_device_location.setText(device.getLocation());
        TextView tv_device_department = (TextView) view.findViewById(R.id.tv_device_department);
        tv_device_department.setText(device.getDepartment());
        return view;
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
